package Test;

import GUI.AvanceGrafica;
import java.awt.*;
import java.util.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.category.*;
import org.jfree.data.category.*;
import javax.swing.*;   

public class BarChart extends SwingWorker<JPanel,Integer>{
    
    static private int avance=0;
    private static AvanceGrafica progreso;   
    ChartPanel chartP;
    float a;
    String titulo;
    int tipo;
    JProgressBar barra;
    
    public BarChart(float a,String st,int type,JProgressBar barra){
        this.a=a;
        titulo=st;
        tipo=type;
        this.barra=barra;
    }
    
public JPanel doInBackground()
{
// Create a simple Bar chart
DefaultCategoryDataset result = new DefaultCategoryDataset();
        result.addValue(a, "Series 1", "Europa BN EN 12021");
        result.addValue(a,  "Series 1", "EE.UU. 20 CFR 1910.134");
        result.addValue(a,  "Series 1", "Canada CSA Z180.1-00");
        result.addValue(a,  "Series 1", "Australia AS/NZS 1715:1994");
switch (tipo){
    case 1:
         result.addValue(22.9,   "Series 2", "Europa BN EN 12021");
        result.addValue(21.8,  "Series 2", "EE.UU. 20 CFR 1910.134");
        result.addValue(19.3,  "Series 2", "Canada CSA Z180.1-00");
        result.addValue(30.3,  "Series 2", "Australia AS/NZS 1715:1994");
        break;
    case 2:
         result.addValue(22.9,   "Series 2", "Europa BN EN 12021");
        result.addValue(21.8,  "Series 2", "EE.UU. 20 CFR 1910.134");
        result.addValue(19.3,  "Series 2", "Canada CSA Z180.1-00");
        result.addValue(30.3,  "Series 2", "Australia AS/NZS 1715:1994");
        break;
    case 3:
        result.addValue(22.9,   "Series 2", "Europa BN EN 12021");
        result.addValue(21.8,  "Series 2", "EE.UU. 20 CFR 1910.134");
        result.addValue(19.3,  "Series 2", "Canada CSA Z180.1-00");
        result.addValue(30.3,  "Series 2", "Australia AS/NZS 1715:1994");
        break;
    case 4:
         result.addValue(22.9,   "Series 2", "Europa BN EN 12021");
        result.addValue(21.8,  "Series 2", "EE.UU. 20 CFR 1910.134");
        result.addValue(19.3,  "Series 2", "Canada CSA Z180.1-00");
        result.addValue(30.3,  "Series 2", "Australia AS/NZS 1715:1994");
        break;
    default:
        JOptionPane.showInternalInputDialog(null, "<html>Error seleccionando<br> Tipo de Grafica</html>", titulo, JOptionPane.ERROR_MESSAGE);
        
}

JFreeChart chart = ChartFactory.createBarChart(titulo,
"Comparativa con las principales normas", "Unidades", result, PlotOrientation.VERTICAL, false,
true, false);

chart.setBackgroundPaint(Color.white);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
BarRenderer renderer = (BarRenderer) plot.getRenderer();    
GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, new Color(0,0,255,135), 0.0f, 0.0f, new Color(0, 0, 64));
 GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.green, 0.0f, 0.0f, new Color(0, 64, 0));
 
  renderer.setSeriesPaint(0, gp0);
  renderer.setSeriesPaint(1, gp1);
  /*try {
ChartUtilities.saveChartAsJPEG(new File("./Barchart.jpg"), chart, 500,
300);
} catch (IOException e) {
System.err.println("Error creando grafico.");
}*/
 
 chartP= new ChartPanel(chart);
// publish(avance);
 /*int i=0;
 while(i<100){
 try{Thread.sleep(10);}catch(Exception e){}
 if(i==50)System.out.println("estoy en pausa");
 i++;}*/
 if(avance>=100)
     avance=0;
 
     avance+=20;
 return (JPanel) chartP;
}

 public void done(){
    System.out.println(avance);
    barra.setValue(avance);
 }
  
    @Override
     protected void process(java.util.List<Integer> chunks) {
        System.out.println(" esta en el hilo "
                + Thread.currentThread().getName());
        try{Thread.sleep(500);}catch(Exception e){}
        int val=chunks.get(0);
    }

}
